﻿/****************************** Namespaces ******************************/
var foodbank = foodbank || {};
foodbank.common = foodbank.common || {};
foodbank.ui = foodbank.ui || {};

/***************************** Localization *****************************/
$(function () {
    //Get culture from meta: accept-language
    var culture = $("meta[name='accept-language']").attr("content");

    //Initialize Client Side Globalization
    Globalize.culture(culture);
    Globalize.addCultureInfo(culture, { messages: window.localizedMessages });

    //Change number validation to take the current culture into account
    $.validator.methods.number = function (value, element) {
        return this.optional(element) || !isNaN(Globalize.parseFloat(value));
    };
    //Set the current language to the request header for ajax calls
    $(document).ajaxSend(function (e, jqxhr) {
        jqxhr.setRequestHeader('accept-language', $("meta[name='accept-language']").attr("content"));
    });
});
//Fix the range validation to take the current culture into account
jQuery.extend(jQuery.validator.methods, {
    range: function (value, element, param) {
        //Use the Globalization plugin to parse the value
        var val = Globalize.parseFloat(value);
        return this.optional(element) || (val >= param[0] && val <= param[1]);
    }
});

/****************************** Error Handling *****************************/
//Show message in console
foodbank.common.showDebug = function (msg) {
    if (window.console && window.console.log)
        window.console.log(msg);
};

/**************************** UI Functions ****************************/
$(function () {
    $('form').each(function () {
        foodbank.ui.setWaterMark($(this));
    });
});

foodbank.ui.setWaterMark = function ($form) {
    $('input[type="text"], input[type="password"]', $form).each(function () {
        var id = $(this).attr('id');
        $(this).attr('placeholder', Globalize.localize(id.toLowerCase()));
    });
};

/************************** Custom Validation ***************************/
$(function () {

    $('form').each(function () {
        var settngs = $.data($(this)[0], 'validator').settings;
        settngs.onkeyup = false;
        settngs.onfocusout = false;
        $(this).unbind("invalid-form.validate"); 
        $(this).bind("invalid-form.validate", function (f, v) {
            if (!$(this).hasClass('no-validation-msg')) {
                for (var s in v.successList) {
                    var $success = $(v.successList[s]);
                    if ($success.next().is('.tipsy')) {
                        $success.next().remove();
                    }
                }
                for (var e in v.errorList) {
                    var error = v.errorList[e];
                    var $errElem = $(error.element);
                    if ($errElem.next().is('.tipsy')) {
                        var errSpan = $errElem.next('.tipsy');
                        errSpan.html(error.message);
                    } else {
                        $errElem.after($('<span class="tipsy">' + error.message + '</span>'));
                    }
                }
            }
        });
    });
});
